
function [p q] = BDF2_Upwinding(dt)
global U  h  g  p  q   nt dx x h1 p_global ;

dx1=1/dx;
dx2 = 1/(dx*dx);
dt2= 1/dt;

beta =0.5;
alpha =0;

rhs = zeros(2*x,1);
sol=rhs;
%---- Construct the spatial Discretization Matrix------%
A = zeros(x,x);

A = zeros(x,x);
A(1,1) = U*dx1;
A(1,x) = -U*dx1;
for k=2:x
    A(k,k) = U*dx1;
    A(k,k-1) = -U*dx1;
end

L=zeros(2*x,2*x);
L(1:x,1:x)=A;
L(x+1:2*x,x+1:2*x)=A;


L(1,2*x)   = h*dx2;
L(1,x+2)   = h*dx2;
L(x,x+1)   = h*dx2;
L(x,2*x-1) = h*dx2;

for k=1:x
    L(k,x+k)= -2*h*dx2;
    L(x+k,k)=g;
end

for k=2:x-1
    L(k,x+k-1) = h*dx2;
    L(k,x+k+1)=  h*dx2;
end
I = eye(2*x);  
% Periodic Boundary Conditions
sol(1:x,1) =p;
sol(x+1:2*x,1) =q;

for n=2:nt+1; 
      
    if (n==2)
    
    rhs = (dt2*I - (1-beta)*L)*sol;
    sol_new=(dt2*I+beta*L)\rhs;        
    
    elseif (n>2)
        rhs = ((dt2*(1+alpha))*I + (alpha - (1-beta))*L)*sol_new - (alpha*dt2)*sol;   
        sol=sol_new;
        sol_new=(dt2*I+beta*L)\rhs;    
        
    end  

        p = sol_new(1:x,1);
        q= sol_new(x+1:2*x,1);
   time = n*dt;
   
    if rem(time,5)==0
        k=time/5;
        p_global(:,k) = p;
        refreshdata(h1,'caller') % Evaluate p in the function workspace
        drawnow
    end

end
display('Completed Successfully');